package com.situ.web.controller;

import com.situ.web.pojo.entity.Course;
import com.situ.web.service.ICourseService;
import com.situ.web.service.impl.CourseServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 浏览器能访问JSP，也可以访问Servlet
 * 和Web开发相关的jar包不在jdk里面，在tomcat里面
 * http://localhost:8080/JavaWeb/hello.jsp
 * http://localhost:8080/JavaWeb/course
 */
@WebServlet("/course")
public class CourseServlet extends HttpServlet {
    private ICourseService courseService = new CourseServiceImpl();

    //访问Servlet时候默认访问service方法
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //解决post请求乱码问题
        req.setCharacterEncoding("UTF-8");
        //http://localhost:8080/course?method=selectAll
        //http://localhost:8080/course?method=deleteById&id=1
        String method = req.getParameter("method");
        if (method == null || method.equals("")) {
            method = "selectAll";
        }
        switch (method) {
            case "selectAll":
                selectAll(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "toAdd":
                toAdd(req, resp);
                break;
            case "add":
                add(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
        }
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.toAdd");
        req.getRequestDispatcher("course_add.jsp").forward(req, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.update");
        String id = req.getParameter("id");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");
        Course course = new Course(Integer.parseInt(id), name, Integer.parseInt(credit));
        courseService.update(course);

        //重定向 302:服务器告诉浏览器去请求这个地址
        resp.sendRedirect("/course?method=selectAll");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.toUpdate");
        String id = req.getParameter("id");
        Course course = courseService.selectById(Integer.parseInt(id));

        req.setAttribute("course", course);
        req.getRequestDispatcher("course_update.jsp").forward(req, resp);
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.add");
        String name = req.getParameter("name");
        String credit = req.getParameter("credit");
        Course course = new Course();
        course.setName(name);
        course.setCredit(Integer.parseInt(credit));

        courseService.add(course);

        //重定向 302:服务器告诉浏览器去请求这个地址
        resp.sendRedirect("/course?method=selectAll");
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("CourseServlet.deleteById");
        String id = req.getParameter("id");
        courseService.deleteById(Integer.parseInt(id));

        //重定向 302:服务器告诉浏览器去请求这个地址
        resp.sendRedirect("/course?method=selectAll");
    }

    private void selectAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CourseServlet.selectAll");
        List<Course> list = courseService.selectAll();
        //把list数据放到req里面
        req.setAttribute("list", list);
        //转发到course_list.jsp页面进行展示
        req.getRequestDispatcher("course_list.jsp").forward(req, resp);
    }
}
